// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2020 Shenshu Technologies CO., LIMITED.
 *
 */
#include "ot_mpi_otp.h"

#include <common.h>
#include "securec.h"
#include "ot_type.h"
#include "drv_otp.h"

static td_bool g_otp_init_flag = TD_FALSE;


#define LOG printf

#define crypto_chk_return(cond, err_ret, fmt, ...) do {     \
    if (cond) {                                             \
        LOG(fmt, ##__VA_ARGS__);                            \
        return err_ret;                                     \
    }                                                       \
} while (0)

#define crypto_otp_not_init_return() do {                   \
    if (g_otp_init_flag != TD_TRUE) {                       \
        LOG("Error: otp not init\n");                       \
            return OT_ERR_OTP_NOT_INIT;                     \
        }                                                   \
} while (0)

td_s32 ot_mpi_otp_init(td_void)
{
    td_s32 ret = TD_SUCCESS;
    if (g_otp_init_flag == TD_TRUE) {
        return TD_SUCCESS;
    }
    ret = drv_otp_init();
    crypto_chk_return(ret != TD_SUCCESS, ret, "drv_otp_init failed, ret is 0x%x\n", ret);

    g_otp_init_flag = TD_TRUE;
    return ret;
}

td_s32 ot_mpi_otp_deinit(td_void)
{
    if (g_otp_init_flag == TD_FALSE) {
        return TD_FAILURE;
    }
    drv_otp_deinit();
    g_otp_init_flag = TD_FALSE;
    return TD_SUCCESS;
}

td_s32 ot_mpi_otp_set_user_data(const td_char *field_name,
    td_u32 offset, const td_u8 *value, td_u32 value_len)
{
    crypto_otp_not_init_return();
    return drv_otp_set_user_data(field_name, offset, value, value_len);
}

td_s32 ot_mpi_otp_get_user_data(const td_char *field_name,
    td_u32 offset, td_u8 *value, td_u32 value_len)
{
    crypto_otp_not_init_return();
    return drv_otp_get_user_data(field_name, offset, value, value_len);
}

td_s32 ot_mpi_otp_set_user_data_lock(const td_char *field_name,
    td_u32 offset, td_u32 value_len)
{
    crypto_otp_not_init_return();
    return drv_otp_set_user_data_lock(field_name, offset, value_len);
}

td_s32 ot_mpi_otp_get_user_data_lock(const td_char *field_name,
    td_u32 offset, td_u32 value_len, ot_otp_lock_status *lock)
{
    crypto_otp_not_init_return();
    return drv_otp_get_user_data_lock(field_name, offset, value_len, lock);
}

td_s32 ot_mpi_otp_burn_product_pv(const ot_otp_burn_pv_item *pv, td_u32 num)
{
    crypto_otp_not_init_return();
    return drv_otp_burn_product_pv(pv, num);
}

td_s32 ot_mpi_otp_read_product_pv(ot_otp_burn_pv_item *pv, td_u32 num)
{
    crypto_otp_not_init_return();
    return drv_otp_read_product_pv(pv, num);
}

td_s32 ot_mpi_otp_get_key_verify_status(const td_char *key_name, td_bool *status)
{
    crypto_otp_not_init_return();
    return drv_otp_get_key_verify_status(key_name, status);
}

